package com.lvt.example.thinking.in.spring.bean.definition;

import com.lvt.example.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * {@link BeanDefinition} 构建示例
 *
 * @author LT
 * @date 2021/6/6
 * @see BeanDefinition
 */
public class BeanDefinitionCreateDemo {

    public static void main(String[] args) {

        // 1. 通过BeanDefinitionBuilder 构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        beanDefinitionBuilder
                .addPropertyValue("id", "1")
                .addPropertyValue("name", "张三");
        BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        // 这里的beanDefinition 并非终态
        System.out.println("BeanDefinitionBuilder get beanDefinitionn is" + beanDefinition);

        // 2. 通过 AbstractBeanDefinition 派生类构建
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("id", "1")
                .add("name", "张三");
        genericBeanDefinition.setPropertyValues(propertyValues);

    }
}
